<?php
/**
 * Created by PhpStorm.
 * User: jurentech
 * Date: 2019-06-18
 * Time: 01:43
 */
namespace app\common\model;

use EasyWeChat\Foundation\Application;
use think\Model;

class Wechat extends Model
{
    public function getaccesstoken()
    {
        $app=new Application(config('wx_config'));
        $accessToken = $app->access_token; // EasyWeChat\Core\AccessToken 实例
        $token = $accessToken->getToken(); // token 字符串
        return $token;
    }



    public function send($id)
    {
        $order=db('order')->where(['order_id'=>$id])->find();
        $url='https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$this->getaccesstoken();
        $data=[
            'touser'=>db('son')->where(['son_id'=>$order['son_id']])->value('openid'),
            'template_id'=>'4n-dda5CxorHbJCtDbBkWFeUjYBFih3ZOcoX0N_QV_s',
            'data'=>[
                'first'=>[
                    'value'=>'【企会计平台分配】'.db('son')->where(['son_id'=>$order['son_id']])->value('son_name').'您好，有新的客户分配给你',
                ],
                'keyword1'=>[
                    'value'=>db('area')->where(['area_id'=>$order['area_id']])->value('area_name'),
                ],
                'keyword2'=>[
                    'value'=>$order['name'],
                ],
                'keyword3'=>[
                    'value'=>$order['phone'],
                ],
                'keyword4'=>[
                    'value'=>date('Y-m-d H:i:s', $order['create_time']),
                ],
                'keyword5'=>[
                    'value'=>$order['name'],
                ],
            ]
        ];

        $this->postcurl($url,$data);
    }

    public function postcurl($url,$data){
        $data=json_encode($data);
        $curl = curl_init();
//设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
//设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
//设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
//post提交的数据
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//执行命令
        $data = curl_exec($curl);
//关闭URL请求
        curl_close($curl);
        return $data;
    }
}